<template>
  <div id="login">
    <div class="login-wrap">
      <ul class="menu-tab">
        <li :class="{'current':item.current}" v-for="item in menuTab" :key="item.id" @click="toggleMenu(item)">{{item.txt}}</li>
      </ul>
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        class="login-form"
        label-position="top"
        size="medium"
      >
        <el-form-item prop="username" class="item-form">
            <label>邮箱</label>
          <el-input type="text" v-model="ruleForm.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item prop="password" class="item-form">
            <label>密码</label>
          <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item prop="password2" class="item-form" v-if="model === 'register'">
            <label>重复密码</label>
          <el-input type="password" v-model="ruleForm.password2" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item prop="code" class="item-form">
            <label>验证码</label>
          <el-row :gutter="10">
            <el-col :span="15">
                <el-input v-model.number="ruleForm.code" minlength = "6" maxlength = "6"></el-input>
            </el-col>
            <el-col :span="9">
                <el-button type="success" class="block">获取验证码</el-button>
            </el-col>
            </el-row>
        </el-form-item>
        <el-form-item>
          <el-button type="danger" @click="submitForm('ruleForm')" class="login-btn block">提交</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import {stripscript} from '@/utils/validate'; 
export default {
  name: "login",
  data() {
    
    var validateUsername = (rule, value, callback) => {
      let reg = /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/
      if (value === "") {
        callback(new Error("请输入用户名"));
      }else if(!reg.test(value)){
        callback(new Error("请输入正确的邮箱地址"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    var validatePassword = (rule, value, callback) => {
        let reg = /^(?!\D+$)(?![^a-zA-Z]+$)\S{6,20}$/;
      if (value === "") {
        callback(new Error("请输入密码"));
      } else if (!reg.test(value)) {
        callback(new Error("密码为6至20位数字+字母"));
      } else {
        callback();
      }
    };


 var validatePassword2 = (rule, value, callback) => {
        let reg = /^(?!\D+$)(?![^a-zA-Z]+$)\S{6,20}$/;
      if (value === "") {
        callback(new Error("请输入确认密码"));
      } else if (value!=this.ruleForm.password) {
        callback(new Error("两次输入密码不一致"));
      } else {
        callback();
      }
    };

    var validateCode = (rule, value, callback) => {
        let reg = /^[a-z0-9]{6}$/;
      if (!value) {
        return callback(new Error("验证码不能为空"));
      }else if(!reg.test(value)){
          return callback(new Error("验证码格式有误"));
      }else{
          callback();
      }
      
    };
    return {
      menuTab: [
          { txt: "登录",'current':false,type:'login' },
          { txt: "注册",'current':true,type:'register' }
          ],
      ruleForm: {
          username: '',
          password: '',
          password2: '',
          code: ''
        },
        model:'reg',
        rules: {
          username: [
            { validator: validateUsername, trigger: 'blur' }
          ],
          password: [
            { validator: validatePassword, trigger: 'blur' }
          ],
          password2: [
            { validator: validatePassword2, trigger: 'blur' }
          ],
          code: [
            { validator: validateCode, trigger: 'blur' }
          ]
        }
    };
  },
  methods:{
      toggleMenu(data){
          this.model = data.type;
        this.menuTab.forEach(element => {
            element.current =false;
            data.current = true;
        });
      },
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            alert('submit!');
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      }
  }
};
</script>

<style lang="scss" scoped>
#login {
  height: 100vh;
  background-color: #344a5f;
}

.login-wrap {
  width: 330px;
  margin: auto;
}

.menu-tab {
  text-align: center;
  li {
    display: inline-block;
    width: 88px;
    line-height: 36px;
    font-size: 14px;
    color: #fff;
    border-radius: 2px;
    cursor: pointer;
  }
}

.current {
  background-color: rgba(0, 0, 1, 0.1);
}
.login-form{
    margin-top: 29px;
    label{
        margin-bottom: 3px;
        display: block;
        font-size: 14px;
        color: #fff;
    }
}
.item-form{
    margin-bottom: 13px;
}
.block{
    display: block;
    width: 100%;
}
.login-btn{
    margin-top: 19px;
}
</style>